﻿using System;
using System.Collections;
using System.Web;

namespace HY.Common
{
    public class WebHelper
    {
        #region Session操作
        /// <summary>
        /// 写Session
        /// </summary>
        /// <param name="key">Session的键名</param>
        /// <param name="value">Session的键值</param>
        public static void SetSession(string key, object value)
        {
            HttpContext.Current.Session[key] = value;
        }

        /// <summary>
        /// 读取Session的值
        /// </summary>
        /// <param name="key">Session的键名</param>        
        public static object GetSession(string key)
        {
            return HttpContext.Current.Session[key];
        }
        /// <summary>
        /// 删除指定Session
        /// </summary>
        /// <param name="key">Session的键名</param>
        public static void RemoveSession(string key)
        {
            HttpContext.Current.Session.Contents.Remove(key);
        }

        #endregion

        #region Cookie操作
        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="key">Cookie的键名</param>
        /// <param name="value">Cookie的键值</param>
        public static void SetCookie(string key, string value)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
            if (cookie == null)
            {
                cookie = new HttpCookie(key);
            }
            cookie.Value = value;
            HttpContext.Current.Response.AppendCookie(cookie);
        }
        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="key">Cookie的键名</param>
        /// <param name="value">Cookie的键值</param>
        /// <param name="expires">过期时间(分钟)</param>
        public static void SetCookie(string key, string value, int expires)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
            if (cookie == null)
            {
                cookie = new HttpCookie(key);
            }
            cookie.Value = value;
            cookie.Expires = DateTime.Now.AddMinutes(expires);
            HttpContext.Current.Response.AppendCookie(cookie);
        }
        /// <summary>
        /// 读cookie值
        /// </summary>
        /// <param name="key">Cookie的键名</param>
        /// <returns>Cookie的键值</returns>
        public static string GetCookie(string key)
        {
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[key] != null)
            {
                return HttpContext.Current.Request.Cookies[key].Value.ToString();
            }
            return "";
        }
        /// <summary>
        /// 删除Cookie对象
        /// </summary>
        /// <param name="key">Cookie的键名</param>
        public static void RemoveCookie(string key)
        {
            HttpCookie objCookie = new HttpCookie(key.Trim());
            objCookie.Expires = DateTime.Now.AddYears(-5);
            HttpContext.Current.Response.Cookies.Add(objCookie);
        }
        #endregion

        #region 缓存操作(使用HttpRuntime.Cache)
        /// <summary>
        /// 获取数据缓存
        /// </summary>
        /// <param name="key">Cache的键名</param>
        public static object GetCache(string key)
        {
            return HttpRuntime.Cache.Get(key);
        }
        /// <summary>
        /// 设置数据缓存(慎用)，会一直占用内存空间，直到系统的虚拟内存或者物理内存超过 System.Web.Caching 所自动设置的上限
        /// </summary>
        /// <param name="key">Key</param>
        /// <param name="value">Value</param>
        public static void SetCache(string key, object value)
        {
            HttpRuntime.Cache.Insert(key, value);
        }
        /// <summary>
        /// 设置数据缓存
        /// </summary>
        /// <param name="key">Key</param>
        /// <param name="value">Value</param>
        /// <param name="seconds">失效时间跨度(秒)</param>
        /// <param name="isAbsoluteExpiration">是否绝对过期，true：绝对过期，到了指定时间以后会失效；false：相对过期，指定时间内无访问会失效</param>
        public static void SetCache(string key, object value, int seconds, bool isAbsoluteExpiration)
        {
            if (isAbsoluteExpiration)
                HttpRuntime.Cache.Insert(key, value, null, DateTime.Now.AddSeconds(seconds), System.Web.Caching.Cache.NoSlidingExpiration);
            else
                HttpRuntime.Cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(seconds));
        }
        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="key">Key为null时，移除全部缓存，否则移除指定Key的缓存</param>
        public static void RemoveCache(string key = null)
        {
            if (key == null)
            {
                System.Web.Caching.Cache cache = HttpRuntime.Cache;
                IDictionaryEnumerator CacheEnum = cache.GetEnumerator();
                while (CacheEnum.MoveNext())
                {
                    cache.Remove(CacheEnum.Key.ToString());
                }
            }
            else
                HttpRuntime.Cache.Remove(key);
        }
        #endregion
    }
}
